package com.bruce.practice.thread.calculate;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

/**
 * @author luotuan
 * @Description
 * @create 2020-05-19 11:32
 **/
public class ExpensiveTask {
    private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(10);

    public static void main(String[] args) {
        ExpensiveCache<String,Integer> expensiveCache = new ExpensiveCache<>(new ExpensiveFunction());
        ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
        IntStream.range(0,10).forEach(i->{
            int local = threadLocalRandom.nextInt(5);
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        expensiveCache.compute(String.valueOf(local));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            EXECUTOR_SERVICE.execute(runnable);
        });
    }
}
